import { resolve } from "path";
import { defineConfig, externalizeDepsPlugin } from "electron-vite";
import react from "@vitejs/plugin-react";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";

export default defineConfig({
  main: {
    resolve: {
      alias: {
        "@resources": resolve("resources"),
      },
    },
    plugins: [externalizeDepsPlugin()],
    build: {
      rollupOptions: {
        input: {
          index: resolve(__dirname, "packages/electron/main/index.ts"),
        },
        // 将 'original-fs' 添加到外部依赖，防止 Vite 打包
        external: ["original-fs"],
      },
    },
  },
  preload: {
    resolve: {
      alias: {
        "@resources": resolve("resources"),
      },
    },
    plugins: [externalizeDepsPlugin()],
    build: {
      rollupOptions: {
        input: {
          index: resolve(__dirname, "packages/electron/preload/index.ts"),
        },
      },
    },
  },
  renderer: {
    root: resolve(__dirname, "packages/renderer-ui"),
    resolve: {
      alias: {
        "@renderer": resolve("packages/renderer-ui"),
        "@src": resolve("packages/renderer-ui/src"),
        "@resources": resolve("resources"),
        "@package/*": resolve("packages/*"),
      },
    },
    server: {
      port: 3000,
    },
    plugins: [
      react(),
      createSvgIconsPlugin({
        iconDirs: [resolve(process.cwd(), "packages/renderer-ui/src/assets/svg")],
        symbolId: "icon-[name]",
      }),
    ],
    build: {
      rollupOptions: {
        input: {
          index: resolve(__dirname, "packages/renderer-ui/index.html"),
        },
      },
    },
  },
});
